"""

This script will take a list of user edits with byte data formatted like, one line per edit, the output of week7/editPrep.py

And output statistics about

for each cohor, and for each lifespan group of that cohort, inlcuding 
amount of work, broken down by kittur's methodology, by date, one for each edit count, time, and bytes changed
proportion of work, broken down by kittur's methodology, by date, one for each edit count, time, and bytes changed      

""" 

import collections
import os
import sys

sys.path.append('src/python/modules')

import wp_actives as wpa

METRICS = ['hours', 'bytes', 'edits']

def main():

    inputDir = sys.argv[1]
    outputDir = sys.argv[2]

    work = {}
    current_work = {}
    
    for year in range(wpa.START_COHORT, wpa.END_COHORT + 1):
        work[year] = {}
        # year -> work metrics
        current_work[year] = collections.defaultdict(empty_work_metrics_dict)
        for offset in range (0, wpa.END_YEAR - year + 1):
            # year -> work metrics
            work[year][offset] = collections.defaultdict(empty_work_metrics_dict)
    
    for is_current, udata, year_props in wpa.WorkYearIterator(inputDir):
        cohort = udata[0]['cohort']
        lifespan = udata[0]['lifespan']
        work_target = current_work[cohort] if is_current else work[cohort][lifespan]
        for year in year_props:
            for m in METRICS:
                for (ns, v) in year_props[year][m + '_prop'].items():
                    work_target[year][m][ns] += v

    current_work_prop = {}
    for cohort in current_work:
        current_work_prop[cohort] = findProportion(current_work[cohort])

    work_prop = {}
    for cohort in work:
        work_prop[cohort] = {}
        for lifespan in work[cohort]:
            work_prop[cohort][lifespan] = findProportion(work[cohort][lifespan])

    
    for cohort in current_work:
        printStats(current_work_prop[cohort], outputDir + '/' + 'cohort'+str(cohort)+'current_prop')
    
    for cohort in work:
        for lifespan in work[cohort]:
            # Make sure its not the last year, because it is always current
            if wpa.END_YEAR - lifespan != cohort:
                printStats(work_prop[cohort][lifespan], outputDir + '/' + 'cohort'+str(cohort)+'lifespan'+str(lifespan)+'_prop')



def printStats(work, basepath):

    header = ['Date', 'Article', 'Article Talk', 'User', 'User Talk', 'Other', 'Maintenance']
    nss = ['article', 'article talk', 'user', 'user talk', 'other', 'maintenance']

    files = {}
    for m in METRICS:
        files[m] = open(basepath + '_' + m + '.stat', 'w')
        files[m].write('\t'.join(header) + '\n')

    for date in sorted(work):
        for m in METRICS:
            tokens = [date]
            for ns in nss:
                tokens.append(str(work[date][m][ns]))
            files[m].write('\t'.join(tokens) + '\n')

    for f in files.values():
        f.close()


def findProportion(data):

    # year ->  ns -> prop
    prop = collections.defaultdict(empty_work_metrics_dict)

    for year in data:
        total_work = {}
        for metric in data[year]:
            total_work[metric] = sum(data[year][metric].values())
        for item in data[year]:
            for metric in data[year]:
                for ns in data[year][metric]:
                    if total_work[metric] == 0:
                        prop[year][metric][ns] = 0
                    else:
                        prop[year][metric][ns] = float(data[year][metric][ns]) / float(total_work[metric])
    return prop
            
def empty_work_metrics_dict():
    return {
        'edits' : collections.defaultdict(float),
        'hours' : collections.defaultdict(float),
        'bytes' : collections.defaultdict(float),
    }


if __name__ == '__main__':
    main()

